function searchStaff(session, str) {
	if (str.length == 0){ 
		str = "all";
	}
	
	var img = document.getElementById('loading-staff');
	img.style.visibility = 'visible';
	
	var xmlhttp = new XMLHttpRequest();
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			img.style.visibility = 'hidden';
		
			var xmlDoc = parseXML(xmlhttp.responseText);
			
			console.debug("Here is the xml document: %o", xmlDoc);
			
			var contacts = xmlDoc.getElementsByTagName("xaresponse")[0].childNodes[1];
						
			if (contacts.nodeName != 'error') 
			{
				createStaffTable(contacts);
			} 
			else 
			{
				clearStaffInfo();
				
				var p = document.createElement('p');
				p.setAttribute('id', 'staff-message');
				p.appendChild(document.createTextNode('No match found.'));
				
				document.getElementById('tabr1').appendChild(p);
			}
		}
	}

	xmlhttp.open("GET","../../apicommfe/search_staff?session=" + session + "&query=" + str, true);
	xmlhttp.send();
}

function createStaffTable(contacts) {
	clearStaffInfo();

	var newTable = document.createElement('table');
	newTable.setAttribute('id', 'staff-table');
	newTable.setAttribute('class', 'sortable');
	
	var attributes = new Array('ID', 'Employee ID', 'Username', 'First Name', 
							   'Last Name', 'Gender', 'Birthday', 'Country', 
							   'City', 'Street Address', 'Mobile', 'Schedule Days',
							   'Schedule Time', 'Role ID');
	
	var tr = document.createElement('tr');
	
	for (var c = 0; c < attributes.length; c++) {
		var tn = document.createTextNode(attributes[c]);
		var td = document.createElement('td');
		td.appendChild(tn);
		tr.appendChild(td);
	}

	newTable.appendChild(tr);
	
	if (contacts.nodeName == 'entitylist')
	{
		for (var c = 0; c < contacts.childElementCount; c++)
		{
			var contact = document.createElement('tr');
			
			for (var a = 0; a < contacts.childNodes[c].childElementCount; a++) {
				var tn = document.createTextNode(contacts.childNodes[c].childNodes[a].childNodes[0].nodeValue);
				var td = document.createElement('td');
				td.appendChild(tn);
				contact.appendChild(td);
			}
			
			newTable.appendChild(contact);
		}
	} 
	else
	{
		var contact = document.createElement('tr');
	
		for (var a = 0; a < contacts.childElementCount; a++) 
		{
			var tn = document.createTextNode(contacts.childNodes[a].childNodes[0].nodeValue);
			var td = document.createElement('td');
			td.appendChild(tn);
			contact.appendChild(td);
		}
		
		newTable.appendChild(contact);
	}
	
	document.getElementById('tabr1').appendChild(newTable);	
}

function clearStaffInfo()
{
	var div   = document.getElementById('tabr1');
	
	var table = document.getElementById('staff-table');
	var p     = document.getElementById('staff-message'); 
	
	if (table != null) {
		div.removeChild(table);
	}
	
	if (p != null) {
		div.removeChild(p);
	}
}